﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Linq;
namespace QLTVNEW
{
    public partial class Form1 : Form
    {
        QLTVDataContext db = new QLTVDataContext();
        public Form1()
        {
            InitializeComponent();

        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            string err = "";
            db.uspCRUDTACGIA('t',0,textBoxX1.Text,textBoxX2.Text,ref err);
            MessageBox.Show(err.ToString());
            db.SubmitChanges();
            Form1_Load(sender, e);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            var query=from tg in db.TACGIAs
                      select tg;
            dataGridViewX1.DataSource = query;
        }

        private void buttonX3_Click(object sender, EventArgs e)
        {
            if (textBoxX3.Text!="")
            {
                string err="";
                db.uspCRUDTACGIA('x', int.Parse(textBoxX3.Text), textBoxX2.Text, textBoxX1.Text, ref err);
                MessageBox.Show(err.ToString());
                db.SubmitChanges();
                Form1_Load(sender, e);
            }
            else
            {
                MessageBox.Show("Chưa nhập Mã Tác Giả");
            }

        }

        private void buttonX2_Click(object sender, EventArgs e)
        {

            if (textBoxX3.Text != "")
            {
                TACGIA tg = db.TACGIAs.Where(t => t.MaTacGia == int.Parse(textBoxX3.Text)).SingleOrDefault<TACGIA>();
                if (tg != null)
                {
                    db.Connection.Open();
                    db.Transaction = db.Connection.BeginTransaction();
                    try
                    {

                        tg.TenTacGia = textBoxX1.Text.Trim();
                        tg.GhiChu = textBoxX2.Text.Trim();
                        db.SubmitChanges();
                        db.Transaction.Commit();
                        MessageBox.Show("Sửa Thành Công");
                        Form1_Load(sender, e);
                    }
                    catch (Exception ex)
                    {
                        db.Transaction.Rollback();
                        MessageBox.Show(ex.Message);
                    }
                    finally
                    {
                        if (db.Connection.State==ConnectionState.Open)
                        {
                            db.Connection.Close();
                        }
                        db.Transaction = null;
                    }
                }
                else
                    MessageBox.Show("Mã TG không tồn tại");
            }
            else
                MessageBox.Show("Chưa Nhập Mã Tác Giả");
           
        }
    }
}
